package com.haina.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.SelectOne;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haina.shop.constant.MessageConstant;
import com.haina.shop.domain.User;
import com.haina.shop.dto.LoginDTO;
import com.haina.shop.dto.UserRegisterDTO;
import com.haina.shop.result.ServiceResult;
import com.haina.shop.service.UserService;
import com.haina.shop.mapper.UserMapper;
import com.haina.shop.vo.LoginVO;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
* @author 48077
* @description 针对表【tz_user(用户表)】的数据库操作Service实现
* @createDate 2024-07-31 18:34:30
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper mapper;
    /**
     * 小程序用户登录
     * @param dto
     * @return
     */
    @Override
    public ServiceResult login(LoginDTO dto) {
        //获取用户的邮箱和密码
        String userMail = dto.getUserName();
        String passWord = dto.getPassWord();
        //创建lambda条件构造器对象
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        //等值查询
        queryWrapper.eq(userMail!=null,User::getUserMail,userMail);
        User user = getOne(queryWrapper);
        if(user==null){
            return ServiceResult.error(MessageConstant.ERROR_CODE,MessageConstant.ACCOUNT_NOT_FOUND);
        }else{
           //比对密码
            String loginPassword = user.getLoginPassword();
            boolean checkpw = BCrypt.checkpw(passWord, loginPassword);
            if(checkpw){
                //用户是否杯锁住
                Integer status = user.getStatus();
                if(status==MessageConstant.USER_STATUS_LOCK){
                    return ServiceResult.error(MessageConstant.ERROR_CODE,MessageConstant.ACCOUNT_LOCKED);
                }else{
                    //成功返回数据
                    return ServiceResult.success(user);
                }
            }else{
                //密码错误
                return ServiceResult.error(MessageConstant.ERROR_CODE,MessageConstant.PASSWORD_ERROR);
            }
        }
    }
    /**
     * 用户注册
     * @param dto
     */
    @Override
    public String register(UserRegisterDTO dto) {
        //获取用户邮箱和密码
        String userMail = dto.getUserName();
        String passWord = dto.getPassWord();
        //密码加密
        String hashpw = BCrypt.hashpw(passWord, BCrypt.gensalt());
        User user = new User(userMail, userMail, userMail, hashpw, passWord, "16763276856",
                new Date(), new Date(), null, null, null, null, "男",
                null, "https://com-haina.oss-cn-beijing.aliyuncs.com/3847f958-5c50-4475-a106-d144ff5f6eca.png",
                1, null);
        //插入数据库
        boolean save = save(user);
        if(save){
            //注册成功
           return user.getUserId();
        }else{
            //注册失败
            return "fail";
        }
    }
}




